/**
 * 客户标签
 */
define(['app', 'js/directive/listTree', 'datetime', 'autocomplete', 'pagination', 'editor'], function(app) {
    'use strict';

    app.controller('Tags', function($scope, dataFactoryCRM, $q) {
        $scope.tags; //检索出来的的标签信息
        $scope.tag; //单个标签信息
        $scope.person //标签关联人数
        $scope.tagcustomer; //标签所关联的用户信息
        $scope.sign;
        $scope.tagRcode;
        $scope.tagMsg;

        $scope.tagsPages = {
            pageAll: 1,
            pageNow: 1,
            pageSize: $scope.psize,
            k: 3
        };
        $scope.customerPages = {
            pageAll: 1,
            pageNow: 1,
            pageSize: $scope.psize,
            k: 3
        };
        $scope.style = {
            objall: "active",
            objmy: "",
        };

        /*标签信息展示*/
        $scope.tagsDataList = function() {
            $scope.$emit('BUSY');
            var _url = 'Tags/index?p=' + $scope.tagsPages.pageNow + '&psize=' + $scope.psize + '&tag_name=' + $('#t_name').val() + '&create_time=' + $('#t_time').val();
            dataFactoryCRM.getDataByAPI(_url).then(function(data) {
                $scope.$emit('NOTBUSY');
                if (data.rcode) {
                    $scope.tagsPages.pageSize = data.psize;
                    $scope.tagsPages.pageAll = data.pall;
                    $scope.tagsPages.pageNow = data.p;
                    $scope.tags = data.data;
                } else {
                    $scope.tagsPages.pageAll = 0;
                    $scope.tags = '';
                }
            });
        };
         //初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
        window.loadClientCacheToScope($scope, function() {
            $scope.tagsDataList();
        });
        /*添加标签*/
        $scope.showAddtags = function() {

            $('.ui.add_tags').modal({
                onApprove: function() {
                    $scope.addTags($('#frm-add').serialize());
                    return true;
                }
            }).modal('show');
        };
        $scope.addTags = function(_d) {
            $scope.$emit('BUSY');
            $scope.busy = dataFactoryCRM.postDataByAPI('Tags/add', _d)
                .then(function(data) {
                    $scope.tagRcode = data.rcode;
                    if (data.rcode) {
                        $('#frm-add input').val('');
                        if ($scope.tags)
                            $scope.tags.splice(0, 0, data.data);
                        else {
                            $scope.tags = new Array();
                            $scope.tags.push(data.data);
                        }
                        $scope.$emit('NOTBUSY');
                    } else {
                        $scope.$emit('NOTBUSY');
                        $scope.tagMsg = data.msg;
                    }
                });
        };
        /*标签编辑操作*/
        $scope.showEdittag = function() {
            var _vtag = this.vtag;
            $('#frm-edit input[name="tag_id"]').val(_vtag.tag_id);
            $('#frm-edit input[name="name"]').val(_vtag.tag_name);
            $('.ui .edit_tags').modal({
                onApprove: function() {
                    if ($('#frm-edit').form('is valid')) {
                        $scope.editTag($('#frm-edit').serialize(), _vtag);
                        return true;
                    } else {
                        return false;
                    }

                }
            }).modal('show');
        };
        $scope.editTag = function(_d, _vtag) {

            /**添加成功 */
            function updateSuccess() {
                setTimeout(function() {
                    $('.modal1').modal('show');
                    //3s后关闭
                    setTimeout(function() {
                        $('.modal1').modal('hide')
                    }, 3000)
                }, 1000);
            }
            /**添加失败 */
            function updateFail() {
                //添加失败了,
                setTimeout(function() {
                    $('.modal2').modal('show');
                    //3s后关闭
                    setTimeout(function() {
                        $('.modal2').modal('hide')
                    }, 3000)
                }, 100);
            }

            $scope.$emit('BUSY');
            $scope.busy = dataFactoryCRM.postDataByAPI('Tags/edit', _d)
                .then(function(data) {
                    $scope.$emit('NOTBUSY');
                    if (data.rcode) {
                        $('#frm-edit input').val('');
                        $scope.tagMsg = '更新成功';
                        updateSuccess();
                        $scope.tagsDataList();
                    } else {
                        $scope.tagMsg = '更新失败';
                        updateFail();
                    }
                });
        };
        /*标签删除操作*/
        $scope.delTag = function() {
            $scope.tag = this.vtag;
            dataFactoryCRM.getDataByAPI('Tags/remove?tag_id=' + $scope.tag.tag_id + '&random=' + 0)
                .then(function(d) {
                    if (d.rcode == 5) {
                        $scope.sign = 1;
                        $scope.tagMsg = "是否确认删除标签【" + $scope.tag.tag_name + "】";
                    } else if (d.rcode == 1) {
                        $scope.sign = 7;
                        $scope.tagMsg = "本标签存在关联用户，是否确认删除？";
                    }
                    $('.ui.modal3').modal({
                        onApprove: function() {
                            $scope.$emit('BUSY');
                            dataFactoryCRM.getDataByAPI('Tags/remove?tag_id=' + $scope.tag.tag_id + '&random=' + $scope.sign)
                                .then(function(data) {
                                    $scope.$emit('NOTBUSY');
                                    if (data.rcode == 3) {
                                        $scope.tagsDataList();
                                    }
                                    $scope.sign = '';
                                });
                            return true;
                        }
                    }).modal('show');

                });
        };
        /*标签关联用户详细信息*/
        $scope.tagCustomer = function() {
            if (this.vtag) {
                $scope.tag = this.vtag;
            }
            var _url = 'Tags/tagCustomer?p=' + $scope.customerPages.pageNow + '&psize=' + $scope.customerPages.pageSize + '&tag_id=' + $scope.tag.tag_id;
            dataFactoryCRM.getDataByAPI(_url).then(function(data) {
                if (data.rcode) {
                    $scope.customerPages.pageSize = data.psize;
                    $scope.customerPages.pageAll = data.pall;
                    $scope.customerPages.pageNow = data.p;
                    $scope.tagcustomer = data.data;
                } else {
                    $scope.tagcustomer = '';
                    $scope.customerPages.pageAll = 0;
                }
            });
            $('.ui.customer').modal('show');
        };
        /*关联用户删除操作*/
        $scope.delCustomer = function() {
            var _vcustomer = this.vcustomer;
            $scope.tagMsg = "确认删除该标签客户 【" + _vcustomer.nickname + "】";
            $('.ui.modal3').modal({
                onApprove: function() {
                    $scope.$emit('BUSY');
                    dataFactoryCRM.getDataByAPI('tags/removeCustomer?id=' + _vcustomer.tags_id + '&tag_id=' + _vcustomer.tag_id)
                        .then(function(d) {
                            $scope.$emit('NOTBUSY');
                            if (d.rcode) {
                                $scope.tag.count = $scope.tag.count - 1;
                                $scope.tagsDataList();
                            }
                        });
                }
            }).modal('show');
        };
        $scope.autoLoading = function() {
            $scope.tagsDataList();
        };
        $scope.autoLoading();
    });

});